package cn.controller;

import cn.entity.Page;
import cn.entity.User;
import cn.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.List;

public class UserDaoAction extends ActionSupport  {
    @Autowired
    @Qualifier("userService")
    private UserService userService;
    private int id;
    private String name;
    private String password;
    private String phone;
    private int perNum;
    private String email;
    private int curPage;//当前页码
    private User user = new User();
    private Page page = new Page();
    public String showList(){

        page.setPerNum(perNum);
        page.setCurPage((curPage-1)*perNum);
        if (name.trim()!=null&&!name.equals("")){
            System.out.println("这里是name的值--"+name+":-----");
            user.setName(name);
        }
        if (phone.trim()!=null&&!phone.equals("")){
            user.setPhone(phone);
        }
        page.setUser(user);
        List<User> userList = userService.getUserList(page);
        System.out.println("拿到信息的条数"+userList.size());
        //拿到总共的信息条数
        int userNum = userService.getUserNum(user);
        if (userNum%perNum==0){
            page.setTotlePage(userNum/perNum);
        }else {
            page.setTotlePage(userNum/perNum+1);
        }
        page.setTotleNum(userNum);
        page.setUserList(userList);
        return SUCCESS;
    }

    public String addUser(){
        user.setName(name);
        user.setPassword(password);
        user.setPhone(phone);
        user.setEmail(email);
        boolean b = userService.addUser(user);
        if (b){
            return SUCCESS;
        }
        return "error";
    }

    public String selectUserbyId(){
        User user1 = new User();
        user1.setId(id);
        user = userService.getUserbyId(user1);
        return SUCCESS;

    }

    public  String deleterUser(){
        User user1 = new User();
        user1.setId(id);
        boolean b =  userService.deleterUser(user1);
       if (b){
           return SUCCESS;
       }
       return "error";
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public int getPerNum() {
        return perNum;
    }

    public void setPerNum(int perNum) {
        this.perNum = perNum;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
